/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.openbookformat.tags;

import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author MUFEED
 */
public class CompositeTag implements Tag {
    private ArrayList<Tag> tags;

    public CompositeTag() {
        tags = new ArrayList<Tag>();
    }

    public Iterator<Tag> iterator() {
        return tags.iterator();
    }

    public void addTag(Tag tag) {
        tags.add(tag);
    }

    public boolean removeTag(Tag tag) {
        return tags.remove(tag);
    }

    public Tag getTag(int index) {
        return tags.get(index);
    }

    public int size() {
        return tags.size();
    }

    public int indexOf(TagType type) {
        return indexOf(type, 0);
    }

    public int indexOf(TagType type, int sFrom) {
        for (int i = sFrom; i < tags.size(); i++)
            if (TagType.BF.getTagType(tags.get(i)).equals(type))
                    return i;
        return -1;
    }

    public void clearTags() {
        tags.clear();
    }

    public void insertTag(int index, Tag tag) {
        tags.add(index, tag);
    }

    @Override
    public String toString() {
        String rVal = "";
        for(int i = 0; i < tags.size(); i++)
            rVal += tags.get(i).toString();
        return rVal;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final CompositeTag other = (CompositeTag) obj;
        if (this.tags != other.tags && (this.tags == null || !this.toString().equals(other.toString()))) {
            return false;
        }
        return true;
    }




}